import { IvcsError } from './error';

export const defaultMediaOptions = {
	cameraStreamAudioConstraints: {
		deviceId: undefined,
		source: 'mic',
	},
	cameraStreamVideoConstraints: {
		deviceId: undefined,
		frameRate: 15,
		resolution: {
			width: 640,
			height: 480,
		},
		source: 'camera',
	},
	cameraPublishOptions: {
		audio: [
			{
				codec: {
					name: 'opus',
				},
				// maxBitrate: 64, fix 网络恢复后对方卡顿问题
			},
		],
		video: [
			{
				codec: {
					name: 'vp8',
				},
				maxBitrate: 1024,
				minBitrate: 600, // 640的经验值600，720的经验值1000
			},
			{
				codec: {
					name: 'h264',
				},
				maxBitrate: 1024,
				minBitrate: 600, // 640的经验值600，720的经验值1000
			},
		],
	},
	screenStreamAudioConstraints: {
		source: 'screen-cast',
	},
	screenStreamVideoConstraints: {
		frameRate: 15,
		resolution: {
			width: 1280,
			height: 720,
		},
		source: 'screen-cast',
	},
	screenPublishOptions: {
		audio: [
			{
				codec: {
					name: 'opus',
				},
				maxBitrate: 64,
			},
		],
		video: [
			{
				codec: {
					name: 'vp8',
				},
				maxBitrate: 1024,
			},
			{
				codec: {
					name: 'h264',
				},
				maxBitrate: 1024,
			},
		],
	},
};

export const defaultLocalStreamParameter = {
	audioConstraints: {
		deviceId: undefined,
		source: 'mic',
	},
	videoConstraints: {
		deviceId: undefined,
		frameRate: 15,
		resolution: {
			width: 640,
			height: 480,
		},
		source: 'camera',
	},
	publishOptions: {
		audio: [
			{
				codec: {
					name: 'opus',
				},
			},
		],
		video: [
			{
				codec: {
					name: 'vp8',
				},
				maxBitrate: 1024,
			},
			{
				codec: {
					name: 'h264',
				},
				maxBitrate: 1024,
			},
		],
	},
};

export const defaultScreenStreamParameter = {
	audioConstraints: {
		source: 'screen-cast',
	},
	videoConstraints: {
		frameRate: 15,
		resolution: {
			width: 1280,
			height: 720,
		},
		source: 'screen-cast',
	},
	publishOptions: {
		audio: [
			{
				codec: {
					name: 'opus',
				},
			},
		],
		video: [
			{
				codec: {
					name: 'vp8',
				},
				maxBitrate: 1024,
			},
			{
				codec: {
					name: 'h264',
				},
				maxBitrate: 1024,
			},
		],
	},
};

export function mediaOptionsIsValid(mediaOptions) {
	if (typeof mediaOptions === 'object') {
		if (!Object.prototype.hasOwnProperty.call(mediaOptions, 'cameraStreamAudioConstraints')) {
			return new IvcsError('105', 'cameraStreamAudioConstraints is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(mediaOptions, 'cameraStreamVideoConstraints')) {
			return new IvcsError('105', 'cameraStreamVideoConstraints is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(mediaOptions, 'cameraPublishOptions')) {
			return new IvcsError('105', 'cameraPublishOptions is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(mediaOptions, 'screenStreamVideoConstraints')) {
			return new IvcsError('105', 'screenStreamVideoConstraints is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(mediaOptions, 'screenPublishOptions')) {
			return new IvcsError('105', 'screenPublishOptions is empty');
		}
		return 'ok';
	} else {
		return new IvcsError('105', 'mediaOptions is not object');
	}
}

export function validateLocalStreamParameter(params) {
	if (typeof params === 'object') {
		if (!Object.prototype.hasOwnProperty.call(params, 'audioConstraints')) {
			return new IvcsError('105', 'audioConstraints is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(params, 'videoConstraints')) {
			return new IvcsError('105', 'videoConstraints is empty');
		}
		if (!Object.prototype.hasOwnProperty.call(params, 'publishOptions')) {
			return new IvcsError('105', 'publishOptions is empty');
		}
		return 'ok';
	} else {
		return new IvcsError('105', 'params is not object');
	}
}

export const ivcsErrorCodeEnum = {
	ERROR_PARAMETER: '1000',
	ERROR_REQUEST_API_FAILED: '4000',
	ERROR_CCS_UNKNOWN: '5000',
	ERROR_CCS_UNDEFINED: '5001',
	ERROR_CCS_TOKEN_UNDEFINED: '5002',
	ERROR_CCS_DISCONNECT: '5003',
	ERROR_CCS_TICKET_INVALID: '5004',
	ERROR_CCS_RESUME_FAILED: '5005',
	ERROR_CCS_SESSION_MESSAGE_FAILED: '5006', // ccs 消息发送失败
	ERROR_CCS_CONNECT_FAILED: '5007', // 
	ERROR_CCS_NOT_EXIST_SESSION: '5008', // ccs 会话不存在
	ERROR_CCS_SENDMESSAGE_DISCONNECTED: '5009', // ccs 发送消息的时候，本端连接断开
	ERROR_CCS_SENDMESSAGE_TIMEOUT: '5010', // ccs发送消息超时
	ERROR_CCS_SENDMESSAGE_ACKTIMEOUT: '5011', // ccs发送消息，ack收到消息超时
	SESSION_IN_TRANSFER:'5012', // 会话正在业务场景转接中
	ERROR_AMS_UNKNOWN: '3000',
	ERROR_AMS_UNDEFINED: '3001',
	ERROR_AMS_TOKEN_UNDEFINED: '3002',
	ERROR_AMS_DISCONNECT: '3003',
	ERROR_AMS_TICKET_INVALID: '3004',
	ERROR_AMS_RESUME_FAILED: '3005',
	ERROR_AMS_API_FAILED: '3006', // ams 消息发送失败
	ERROR_AMS_CONNECT_FAILED: '3007', // ams 连接失败
	ERROR_AMS_MESSAGE_TIMEOUT: '3008', // ams 消息发送超时
	ERROR_AMS_RECONNECTING: '3009', // ams reconnecting or disconnected
	ERROR_ZMS_IRTC_UNDEFINED: '6000',
	ERROR_ZMS_STREAMLABEL_INVALID: '60001',
	ERROR_ZMS_JOINROOM_FAILED: '6001',
	ERROR_ZMS_CREATESTREAM_FAILED: '6002',
	ERROR_ZMS_ABORTERROR: '60021',
	ERROR_ZMS_NOTALLOWEDERROR: '60022',
	ERROR_ZMS_NOTFOUNDERROR: '60023',
	ERROR_ZMS_NOTREADABLEERROR: '60024',
	ERROR_ZMS_OVERCONSTRAINEDERROR: '60025',
	ERROR_ZMS_SECURITYERROR: '60026',
	ERROR_ZMS_TYPEERROR: '60027',
	ERROR_ZMS_SCREEN_NOT_SUPPORT_TAB: '60028',
	ERROR_ZMS_PUBLISH_FAILED: '6004',
	ERROR_ZMS_SUBSCRIBE_FAILED: '6005',
	ERROR_ZMS_VIDEOPLAY_FAILED: '6007',
	ERROR_ZMS_RENDERVIDEO_NOTEXIST: '6006',
	ERROR_ZMS_MUTE_AUDIO_FAILED: '60070',
	ERROR_ZMS_MUTE_VIDEO_FAILED: '60071',
	ERROR_ZMS_UNMUTE_AUDIO_FAILED: '60080',
	ERROR_ZMS_UNMUTE_VIDEO_FAILED: '60081',
	ERROR_SDK_CATCH: '7000',
};
